let page = 1;
const pageSize = 9;
var logInfo = {};
let table_data = [];

var maxCount;

window.onload = initTable;

// 初始化表格
function initTable() {
    getPageData(1);
}

// 分页查询
function getPageData(num) {
    App.postJson(App.basePath + "/log/page", JSON.stringify({
            "limit": pageSize,
            "orderBys": [
                {
                    "asc": true,
                    "orderField": ""
                }
            ],
            "page": num,
            "param": {
                "content": null,
                "userID": null,
                "userName": null

            }
        })
        , function (res) {
            table_data = null;
            table_data = res.data;
            maxCount = Math.ceil(res.count / 9);
            changeTable();
        }
        , function (res) {
        }, function () {

        });
}

//首页
function goFirstPageLog() {
    const elementsByName = document.getElementsByName("pageSpan");
    for (let i = 0; i <elementsByName.length ; i++) {
        elementsByName[i].innerText = i+1;
    }
    page = 1;
    getPageData(page);
}

//尾页
function goEndPageLog() {
    const elementsByName = document.getElementsByName("pageSpan");
    for (let i = 0; i <elementsByName.length ; i++) {
        elementsByName[i].innerText = maxCount+i-elementsByName.length +1;
    }
    page = maxCount;
    getPageData(page);
}

//上下翻页
function up_down_page(flag) {
    if (flag === false) {
        page++;
    } else if (page !== 1) {
        page--;
    }
    page = page >= maxCount ? maxCount : page;
    const elementsByName = document.getElementsByName("pageSpan");
    let minPage = document.getElementById("minPage").innerText;
    let maxPage = document.getElementById("maxPage").innerText;
    if (page > maxPage) {
        for (let i = 0; i < elementsByName.length; i++) {
            elementsByName[i].innerText = parseInt(elementsByName[i].innerText) + parseInt(6);
        }
    }
    if (page < minPage) {
        for (let i = 0; i < elementsByName.length; i++) {
            elementsByName[i].innerText = parseInt(elementsByName[i].innerText) - parseInt(6);
        }
    }
    getPageData(page);
}

//数字键翻页
function goPage(element) {
    page = element.innerText;
    page = page >= maxCount ? maxCount : page;
    getPageData(page);
}

//改变翻页按钮的颜色
function changeButColor() {
    const elementsByName = document.getElementsByName("pageSpan");
    for (let i = 0; i < elementsByName.length; i++) {
        if (elementsByName[i].innerText == page) {
            elementsByName[i].style.backgroundColor = "rgba(29,43,85,0.6)";
        } else {
            elementsByName[i].style.backgroundColor = null;
        }
    }
}

// 改变表格的数据
function changeTable() {
    const tableData = document.getElementById("logInfo");
    $("#logInfo  td:not(:first)").empty("");
    let tempClassMethod="null";
    for (let i = 0; i < table_data.length; i++) {
        if (table_data[i].classMethod != null && table_data[i].classMethod.length > 40) {
            tempClassMethod = "..."+table_data[i].classMethod.substring(20);
        }
        tableData.rows[i + 1].cells[0].innerHTML = table_data[i].userName;
        tableData.rows[i + 1].cells[1].innerHTML = table_data[i].ipAddress;
        tableData.rows[i + 1].cells[2].innerHTML = table_data[i].client;
        tableData.rows[i + 1].cells[3].innerHTML = table_data[i].content;
        tableData.rows[i + 1].cells[4].innerHTML = table_data[i].url;
        tableData.rows[i + 1].cells[5].innerHTML = table_data[i].httpMethod;
        tableData.rows[i + 1].cells[6].innerHTML = tempClassMethod;
        tableData.rows[i + 1].cells[7].innerHTML = table_data[i].operateTime;
        tableData.rows[i + 1].cells[8].innerHTML = "<img id='" + i + "' class='opSty'   src=\"/images/backgrounds/logInfo.png\" alt=\"\" onclick='getLogInfo(this)' data-toggle='modal' data-target='#myLogModal'/>";
    }
    changeButColor();
}

//获取日志相信信息
function getLogInfo(element) {
    logInfo = table_data[parseInt(element.id)];
    document.getElementById("log_userId").innerText = logInfo.userId;
    document.getElementById("log_userName").innerText = logInfo.userName;
    document.getElementById("log_ip").innerText = logInfo.ipAddress;
    document.getElementById("log_client").innerText = logInfo.client;
    document.getElementById("log_content").innerText = logInfo.content;
    document.getElementById("log_url").innerText = logInfo.url;
    document.getElementById("log_httpMethod").innerText = logInfo.httpMethod;
    document.getElementById("log_classMethod").innerText = logInfo.classMethod;
    document.getElementById("log_param").innerText = logInfo.parameters;
    document.getElementById("log_result").innerText = logInfo.result;
    document.getElementById("log_time").innerText = logInfo.operateTime;
}